package com.rabbitmq.demo.service.impl;


import com.rabbitmq.demo.dao.UserDao;
import com.rabbitmq.demo.pojo.User;
import com.rabbitmq.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;


    @Override
    public boolean register(String email, String password) {

        boolean registered = isRegistered(email);
        if (registered){
            return false;
        }
        Date dt = new Date();
        User user = User.builder().email(email)
                .password(password)
                .createTime(dt)
                .loginTime(dt)
                .build();
        userDao.save(user);
        log.info(String.format("用户：%s 注册成功！", email));
        return true;
    }

    @Override
    public boolean isRegistered(String email) {
        User user = userDao.findUserByEmail(email);
        return user != null;
    }

    @Override
    public User findUserByEmail(String email) {
        return userDao.findUserByEmail(email);
    }

}
